package com.happyclub.lbj.service.impl;

import com.happyclub.lbj.service.IProductColor;
import com.happyclub.lbjdao.ProductColor;
import com.happyclub.pojo.TbProductColor;
import com.happyclub.utils.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;

import java.util.List;

import static com.happyclub.errormsg.ErrorMsgProvider.SYSTEM_BUSY;

/**
 * @author 高欢
 * @description TODO
 * @date 2019/4/10
 */
@Service
public class ProductColorImpl implements IProductColor {
    @Autowired
    ProductColor iproColor;

    @Transactional
    @Override
    public JsonResult saveProductColor(TbProductColor productColor) {
        JsonResult<Object> jsonResult = new JsonResult<>();
        TbProductColor save = iproColor.save(productColor);
        if (save==null){
            jsonResult.setMsg(SYSTEM_BUSY);
            jsonResult.setErrorCode("500");
            return jsonResult;
        }
        return jsonResult;
    }

    @Transactional
    @Override
    public JsonResult delProductColor(String id) {
        JsonResult<Object> jsonResult = new JsonResult<>();
        iproColor.deleteById(id);
        return jsonResult;
    }

    @Transactional
    @Override
    public JsonResult updateProductColor(TbProductColor productColor) {
        JsonResult<Object> jsonResult = new JsonResult<>();
        int i = iproColor.updateColor(productColor.getColor(), productColor.getId());
        if (i<=0){
            jsonResult.setErrorCode("500");
            jsonResult.setMsg(SYSTEM_BUSY);
            return jsonResult;
        }
        return jsonResult;
    }

    @Override
    public JsonResult findAllProductColor() {
        JsonResult<Object> jsonResult = new JsonResult<>();
        List<TbProductColor> all = iproColor.findAll();
        if (all.size()==0){
            jsonResult.setMsg("目前还没有颜色");
            return jsonResult;
        }
        jsonResult.setResult(all);
        return jsonResult;
    }
}
